package com.ycsj.goldmedalnewconcept.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.text.format.Time;

/**
 * 创建日期：2017/11/13  18:12
 * 描述:时间格式化工具类
 * 作者:张伟
 */

public class TimeUtil {
	private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年
    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

    /**
     * 将yyyy-MM-dd HH:mm:ss转化未yyyy-MM-dd
     */
    public static String getMonthDay(String time) {
        String res = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            Date date = sdf.parse(time);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            res = df.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }
    /**
     * 将string转Long
     */
    public static long stringToLong(String strTime, String formatType)
            throws ParseException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }
    
    public static long dateToLong(Date date) {
        return date.getTime();
    }
    
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }
    
    
    /**
     * 判断原日期是否在目标日期之后
     * 
     * @param src
     * @param dst
     * @return
     */
    public static boolean isAfter(String src, String dst) {
    	
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
         
             Date date1 = null;
             Date date2 = null;
			try {
				date1 = sdf.parse(src);
				date2 = sdf.parse(dst);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
         
        return date1.after(date2);
    }
    
    public static String getYear() {  
        String timeString = null;  
        Time time = new Time();  
        time.setToNow();  
  
        return time.year+"";  
    }  
    public static String getMonth() {  
    	String timeString = null;  
    	Time time = new Time();  
    	time.setToNow();  
    	String month = time.month + 1+"";  
    	
    	return month;  
    }  
    public static String getDay() {  
    	String timeString = null;  
    	Time time = new Time();  
    	time.setToNow();  
    	String monthDay = time.monthDay+"";  
    	
    	// System.out.println("-------timeString----------" + timeString);  
    	return monthDay;  
    }  
    
    
    
    
    
    
    
    
    
    
    
    /**
     * 判断原日期是否在目标日期之后
     * 
     * @param src
     * @param dst
     * @return
     */
    public static boolean isTodayAfterbegin(String src) {
    	
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
         
             Date date2 = null;
             Date date1 = new Date(System.currentTimeMillis()+8*60*60*1000);
			try {
				date2 = sdf.parse(src);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
         
        return date1.after(date2);
    }
    /**
     * 判断原日期是否在目标日期之后
     * 
     * @param src
     * @param dst
     * @return
     */
    public static boolean isTodayAfterend(String src) {
    	
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
    	
    	Date date2 = null;
    	Date date1 = new Date(System.currentTimeMillis()+9*60*60*1000);
    	try {
    		date2 = sdf.parse(src);
    	} catch (ParseException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	
    	return date1.after(date2);
    }
    /**
     * 判断日期是否在今天以前
     * 
     * @param src
     * @param dst
     * @return
     */
    public static boolean isTodayAfter(String src) {
    	
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    	
    	Date date2 = null;
    	Date date1 = new Date(System.currentTimeMillis()-24*60*60*1000);
    	try {
    		date2 = sdf.parse(src);
    	} catch (ParseException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	
    	return date1.after(date2);
    }
    
    
    /**
     * 判断日期是否在当前时间以前
     * 
     * @param src
     * @param dst
     * @return
     */
    public static boolean isNowAfter(String src) {
    	
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
    	
    	Date date2 = null;
    	Date date1 = new Date(System.currentTimeMillis());
    	try {
    		date2 = sdf.parse(src);
    	} catch (ParseException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	
    	return date1.after(date2);
    }
    
    
    /**
     * 将yyyy-MM-dd HH:mm:ss转化未MM/dd
     */
    public static String getMD(String time) {
        String res = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            Date date = sdf.parse(time);
            SimpleDateFormat df = new SimpleDateFormat("MM/dd", Locale.CHINA);
            res = df.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    /**
     * String 转 Data
     */
    public static Date stringConvertDate(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        Date data = null;
        try {
            data = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 返回发布时间距离当前的时间
     */
    public static String timeAgo(Date createdTime) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA);
        if (createdTime != null) {
            long agoTimeInMin = (new Date(System.currentTimeMillis()).getTime() - createdTime.getTime()) / 1000 / 60;
            // 如果在当前时间以前一分钟内
            if (agoTimeInMin <= 1) {
                return "刚刚";
            } else if (agoTimeInMin <= 60) {
                // 如果传入的参数时间在当前时间以前10分钟之内
                return agoTimeInMin + "分钟前";
            } else if (agoTimeInMin <= 60 * 24) {
                return agoTimeInMin / 60 + "小时前";
            } else if (agoTimeInMin <= 60 * 24 * 2) {
                return agoTimeInMin / (60 * 24) + "天前";
            } else {
                return format.format(createdTime);
            }
        } else {
            return format.format(new Date(0));
        }
    }

    /**
     * 根据时间戳 返回发布时间距离当前的时间
     */
    public static String getTimeStampAgo(String timeStamp) {
        Long time = Long.valueOf(timeStamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String string = sdf.format(time * 1000L);
        Date date = null;
        try {
            date = sdf.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeAgo(date);
    }

    public static String getCurrentTimeStamp() {
        return String.valueOf(System.currentTimeMillis() / 1000);
    }
    
    
    /**
     * 返回文字描述的日期
     *
     * @param date
     * @return
     */
    public static String getTimeFormatText(String time) {
        if (time == null) {
            return null;
        }
        
        SimpleDateFormat sdf = new SimpleDateFormat(("yyyy-MM-dd HH:mm:ss"));
        Date date = null;
        try {
        date = sdf.parse(time);
        } catch (ParseException e) {
        e.printStackTrace();
        } 
//        System.out.println(new Date(date.getTime()));
        
        
        long diff = new Date().getTime() - date.getTime();
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }
    
    public static String getYearMonth() {  
        Date date = new Date();// 创建一个时间对象，获取到当前的时间  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 设置时间显示格式  
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置时间显示格式  
        String CurrentTime = sdf.format(date);  
  
        return CurrentTime;  
    }
    public static String getYearMonthDay() {  
    	Date date = new Date();// 创建一个时间对象，获取到当前的时间  
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 设置时间显示格式  
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置时间显示格式  
    	String CurrentTime = sdf.format(date);  
    	
    	return CurrentTime;  
    }
    
    
    
    public static String getTimetoday(String day) throws ParseException {
 
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return "今天";
            }
        }
        return "-1";
    }
    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }
}
